package com.rdc.qingkou.model;

import android.util.Log;

import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
import com.rdc.qingkou.contract.MessageContract;
import com.rdc.qingkou.entity.MessageNewsNumResult;
import com.rdc.qingkou.entity.MessageResult;
import com.rdc.qingkou.model.imodel.OnHttpCallBack;
import com.rdc.qingkou.retrofitService.IMessageService;
import com.rdc.qingkou.utils.MyUtils;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by WaxBerry on 2017/10/15.
 */

public class MessageModel implements MessageContract.IMessageModel {
    @Override
    public void getUnReadNewsNum(Integer userId,final OnHttpCallBack<MessageNewsNumResult> callBack) {

        MyUtils.getRetrofitProvider("")
                .create(IMessageService.class)
                .getUnReadNewsNum(userId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread())
                .subscribe(new Observer<MessageNewsNumResult>() {

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.d("MessageModel","onSubscribe");
                    }

                    @Override
                    public void onNext(MessageNewsNumResult messageNewsNumResult) {
                        Log.d("MessageModel","onNext");
                        callBack.onSuccessful(messageNewsNumResult);
                    }

                    @Override
                    public void onError(Throwable t) {
                        Log.d("MessageModel","onError");
                        t.printStackTrace();
                        //失败的时候回调-----一下可以忽略 直接 callBack.onFaild("请求失败");
                        if (t instanceof HttpException) {
                            HttpException httpException = (HttpException) t;
                            int code = httpException.code();
                            if (code == 500 || code == 404) {
                                callBack.onFaild("服务器出错");
                            }
                        } else if (t instanceof ConnectException) {
                            callBack.onFaild("网络断开,请打开网络!");
                        } else if (t instanceof SocketTimeoutException) {
                            callBack.onFaild("网络连接超时!!");
                        } else {
                            callBack.onFaild("发生未知错误" + t.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {
                        Log.d("MessageModel","onComplete");
                    }
                });
    }

    @Override
    public void getMessage(Integer userId,Integer currentPage,final OnHttpCallBack<MessageResult> callBack) {

        MyUtils.getRetrofitProvider("")
                .create(IMessageService.class)
                .getNews(userId,currentPage)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread())
                .subscribe(new Observer<MessageResult>() {

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(MessageResult messageResult) {
                        callBack.onSuccessful(messageResult);
                    }

                    @Override
                    public void onError(Throwable t) {
                        t.printStackTrace();
                        //失败的时候回调-----一下可以忽略 直接 callBack.onFaild("请求失败");
                        if (t instanceof HttpException) {
                            HttpException httpException = (HttpException) t;
                            int code = httpException.code();
                            if (code == 500 || code == 404) {
                                callBack.onFaild("服务器出错");
                            }
                        } else if (t instanceof ConnectException) {
                            callBack.onFaild("网络断开,请打开网络!");
                        } else if (t instanceof SocketTimeoutException) {
                            callBack.onFaild("网络连接超时!!");
                        } else {
                            callBack.onFaild("发生未知错误" + t.getMessage());
                            //KLog.e(t.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void getMoreMessage(Integer userId, Integer currentPage,final OnHttpCallBack<MessageResult> callBack) {

        MyUtils.getRetrofitProvider("")
                .create(IMessageService.class)
                .getMoreNews(userId,currentPage)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread())
                .subscribe(new Observer<MessageResult>() {

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(MessageResult messageResult) {
                        callBack.onSuccessful(messageResult);
                    }

                    @Override
                    public void onError(Throwable t) {
                        t.printStackTrace();
                        //失败的时候回调-----一下可以忽略 直接 callBack.onFaild("请求失败");
                        if (t instanceof HttpException) {
                            HttpException httpException = (HttpException) t;
                            //httpException.response().errorBody().string()
                            int code = httpException.code();
                            if (code == 500 || code == 404) {
                                callBack.onFaild("服务器出错");
                            }
                        } else if (t instanceof ConnectException) {
                            callBack.onFaild("网络断开,请打开网络!");
                        } else if (t instanceof SocketTimeoutException) {
                            callBack.onFaild("网络连接超时!!");
                        } else {
                            callBack.onFaild("发生未知错误" + t.getMessage());
                            //KLog.e(t.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    @Override
    public void getUpdateNews(Integer userId) {

    }
}
